/****************************************************************************
 * 2024.9 ADMIN-20230317Z
 ****************************************************************************/

using System;
using System.Collections.Generic;
using cfg.level;
using UnityEngine;
using UnityEngine.UI;
using QFramework;
using UnityEngine.U2D;

namespace QFramework.Monk
{
	public partial class SkillPanel : UIElement
	{
		private Dictionary<string,SkillUIItem> mSkillItemDic = new Dictionary<string, SkillUIItem>();
		private ResLoader  resLoader;
		private SpriteAtlas atlas;
		private void Awake()
		{
			mSkillItemDic.Clear();
			resLoader = ResLoader.Allocate();
			atlas = resLoader.LoadSync<SpriteAtlas>("Icon");
		}

		protected override void OnBeforeDestroy()
		{
			mSkillItemDic.Clear();
			resLoader.Recycle2Cache();
			resLoader = null;
			atlas = null;
		}

		public void RefshSkillItem(SkillItem item)
		{
			var key = item.SkillType.ToString();
			if (!mSkillItemDic.ContainsKey(key))
			{
				var skillUIItem = skillItem.InstantiateWithParent(this.transform).Self(self =>
				{
					var selfCache = self;
					self.SetImage(atlas.GetSprite(item.Icon));
					self.RefreshLv(item.SkillType);
				}).Show();
				mSkillItemDic.Add(key,skillUIItem);
			}
			else
			{
				mSkillItemDic[key].RefreshLv(item.SkillType);
			}
			
		}
	}
}